/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package relatorios.presenter;

import br.com.webhomesites.padrao.model.ClienteDAO;
import br.com.webhomesites.padrao.model.ClienteModel;
import br.com.webhomesites.padrao.model.RelatorioLocPorClienteModel;
import br.com.webhomesites.padrao.presenter.ClientePresenter;
import br.com.webhomesites.padrao.utils.Strategy;
import br.com.webhomesites.principal.EmissaoRelatorios;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import relatorios.view.RelLocPorCLiView;
import relatorios.view.RelLocPorProdView;

/**
 *
 * @author Webhome
 */
public class RelatoriosPresenter {

    ClientePresenter produtoPresenter;
    ClienteModel clienteModel = new ClienteModel();
    ClienteDAO clienteDAO = new ClienteDAO();
    RelLocPorCLiView relLocPorCLiView;
    RelLocPorProdView relLocPorProdView;
    RelatorioLocPorClienteModel relatorioLocPorCliModel = new RelatorioLocPorClienteModel();
    public Strategy operacao;
    public final Strategy REL_LOC_POR_CLI_STRATEGY = new RelLocPorCliStrategy();
    DecimalFormat df = new DecimalFormat("###,###,###.00");
    DecimalFormat df2 = new DecimalFormat("###,###,###.0");
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

    public RelatoriosPresenter(RelLocPorCLiView relLocPorCliView, RelLocPorProdView relLocPorProdView) {
        this.relLocPorCLiView = relLocPorCliView;
        this.relLocPorProdView = relLocPorProdView;
        setUpViewListeners();
    }

    public void setUpViewListeners() {
        
        if(relLocPorCLiView!=null) {
        relLocPorCLiView.setEmitirRelatorioActionlistener(new RelatoriosPresenterActionListener.EmitirRelLocPorCliListener(this));
        relLocPorCLiView.setCampoKeyListener(new RelatoriosPresenterActionListener.CampoKeyListener(this));
        } else if (relLocPorProdView!=null) {
            
        }
    }

    public void updateRelatorioLocPorCliModelFromView() {
        try {
            relatorioLocPorCliModel.setDataInicial((Date) formatter.parse(relLocPorCLiView.getDataInicial()));
            relatorioLocPorCliModel.setDataFinal((Date) formatter.parse(relLocPorCLiView.getDataFinal()));
            relatorioLocPorCliModel.setClienteModel(clienteDAO.findByDescricaoUnica(relLocPorCLiView.getNomeCliente()));
        } catch (ParseException ex) {
            Logger.getLogger(RelatoriosPresenter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public RelLocPorCLiView getRelLocPorCliView() {
        return relLocPorCLiView;
    }
    
    public void setRelLocPorCliView(RelLocPorCLiView relLocPorCLiView) {
        this.relLocPorCLiView = relLocPorCLiView;
    }

    public RelLocPorProdView getRelLocPorProdView() {
        return relLocPorProdView;
    }

    public void setRelLocPorProdView(RelLocPorProdView relLocPorProdView) {
        this.relLocPorProdView = relLocPorProdView;
    }    

    public RelatorioLocPorClienteModel getRelatorioLocPorClienteModel() {
        return relatorioLocPorCliModel;
    }

    public void setRelatorioLocPorClienteView(RelLocPorCLiView relLocPorCLiView) {
        this.relLocPorCLiView = relLocPorCLiView;
    }

    public void setRelatorioLocPorClienteModel(RelatorioLocPorClienteModel relatorioLocPorCliModel) {
        this.relatorioLocPorCliModel = relatorioLocPorCliModel;
    }

    public void createView(String tipoView) {

        switch (tipoView) {
            case "locacoesPorCliente":
                relLocPorCLiView.packAndShow();
                break;
            case "locacoesPorProduto":
                relLocPorProdView.packAndShow();
                break;
        }
    }

    public void setOperacao(Strategy operacao) {
        this.operacao = operacao;
    }

    public Strategy getOperacao() {
        return operacao;
    }

    private class RelLocPorCliStrategy implements Strategy {

        @Override
        public void execute() {

            updateRelatorioLocPorCliModelFromView();

            try {
                Long n = relatorioLocPorCliModel.getClienteModel().getId();
                Integer n1 = Integer.valueOf(n.toString());
                EmissaoRelatorios emissaoRelatorios = new EmissaoRelatorios();

                emissaoRelatorios.locacoesPorCliente(n1, relatorioLocPorCliModel.getDataInicial(), relatorioLocPorCliModel.getDataFinal());

            } catch (Exception e) {
                System.out.println("Capacidade do Integer estourou.");
            }
        }
    }
}
